package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetMealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "套餐管理")
@RequestMapping("/admin/setmeal")
@Slf4j
public class SetMealController {

    @Autowired
    private SetMealService setMealService;
    //新增套餐,然后根据categoryId将数据进行缓存进入redis
    @ApiOperation("新增套餐")
    @CacheEvict(cacheNames = "setMealCache",allEntries = true)
    @PostMapping
    public Result saveSetMeal(@RequestBody SetmealDTO setmealDTO){
        log.info("新增套餐{}",setmealDTO);
        setMealService.saveSetMeal(setmealDTO);
        return Result.success();
    }
    //条件分页查询
    @ApiOperation("套餐分页查询")
    @GetMapping("/page")
    public Result<PageResult> queryByPage(SetmealPageQueryDTO setmealPageQueryDTO){
        log.info("套餐分页查询");
        PageResult pageResult = setMealService.queryByPage(setmealPageQueryDTO);
        return Result.success(pageResult);
    }
    //套餐起售，停售套餐
    @ApiOperation("套餐起售与停售")
    @CacheEvict(cacheNames = "setMealCache",allEntries = true)
    @PostMapping("/status/{status}")
    public Result startOrStop (@PathVariable Integer status,Long id){
        setMealService.startOrStop(status,id);
        return Result.success();
    }
    //根据套餐的id查询套餐
    @ApiOperation("根据id查询套餐")
    @GetMapping("/{id}")
    public Result<SetmealVO> queryById(@PathVariable Long id){
        SetmealVO setmealVO = setMealService.queryById(id);
        return Result.success(setmealVO);
    }
    //套餐修改
    @ApiOperation("套餐修改")
    @CacheEvict(cacheNames = "setMealCache",allEntries = true)
    @PutMapping
    public Result updateById (@RequestBody SetmealDTO setmealDTO){
        setMealService.updateById(setmealDTO);
        return Result.success();
    }
    //批量删除套餐
    @ApiOperation("批量删除套餐")
    @CacheEvict(cacheNames = "setMealCache",allEntries = true)
    @DeleteMapping
    public Result deleteByIds(@RequestParam List<Long> ids){
        setMealService.deleteByIds(ids);
        return Result.success();
    }

}
